package fregat.base.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Base class for classes that define the protocol how data is received and sent.
 *
 * @author Alexander Mann <br><b><i>ECO_L O G</i> &nbsp Logistiksysteme GmbH</b>
 * @version 1.0
 */
public abstract class Protocol {
  private String name;

  /**
   * Creates a protocol with the given name.
   *
   * @param name Name of instance that uses the protocol
   */
  public Protocol(String name) {
    this.name = name;
  }

  /**
   * @return Name of instance that uses the protocol
   */
  public String getName() {
    return name;
  }

  /**
   * Receives data from the input reader.
   *
   * @param in Input stream
   * @return Received data(including header)
   * @throws IOException
   */
  public abstract byte[] receive(InputStream in) throws IOException;

  /**
   * Sends the given data to the output stream.
   *
   * @param out      Output stream
   * @param sendData Data to be sent
   * @return True, if send was success
   */
  public abstract boolean send(OutputStream out, byte[] sendData);
  
  public boolean isBinary(){ return false; }

  /**
   * Connection was closed.
   */
  public abstract void close();
}